﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ODP.Helper;

namespace ODP.SinaCrawler
{
    public static class Helper
    {
        #region 根据序号获取指定的AppKey
        /// <summary>
        /// 根据序号获取指定的AppKey
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public static string GetAppKey(int index)
        {
            string key = "";
            var results = Properties.Settings.Default.Properties;
            foreach (System.Configuration.SettingsProperty item in results)
            {
                if (item.Name.Contains("AppKey") && item.Name.Contains(index.ToString()))
                {
                    key = Convert.ToString(item.DefaultValue);
                    break;
                }
            }
            return key;
        }
        #endregion

        #region 根据序号获取指定的AppSecret
        /// <summary>
        /// 根据序号获取指定的AppSecret
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public static string GetAppSecret(int index)
        {
            string secret = "";
            var results = Properties.Settings.Default.Properties;
            foreach (System.Configuration.SettingsProperty item in results)
            {
                if (item.Name.Contains("AppSecret") && item.Name.Contains(index.ToString()))
                {
                    secret = Convert.ToString(item.DefaultValue);
                    break;
                }
            }
            return secret;
        }
        #endregion

        #region 重置当前登陆的appkeyindex
        /// <summary>
        /// 重置当前登陆的appkeyindex
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public static int SetAppKeyIndex(int index)
        {
            if (index < 5)
            {
                index++;
            }
            else
            {
                index = 1;
            }

            return index;
        }
        #endregion

        #region 获取webbrowser的url参数
        /// <summary>
        /// 获取webbrowser的url参数
        /// </summary>
        /// <param name="urlquery"></param>
        /// <param name="queryname"></param>
        /// <returns></returns>
        internal static string GetQueryString(string urlquery, string queryname)
        {
            string result = string.Empty;
            string[] para;
            urlquery = urlquery.Replace("?", "");

            string[] namevalues = urlquery.Split("&".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            foreach (string namevalue in namevalues)
            {
                para = namevalue.Split("=".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                if (para[0] == queryname)
                {
                    result = para[1];
                    break;
                }
            }

            return result;
        }
        #endregion
    }
}
